package com.kongming.kmdm.ws.rest.config;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;

import com.kongming.utils.misc.Tools;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

public class WSConfig {

    private static Properties properties;

    private static final String CONFIG_FILE = "conf/config.properties";

    private static final String MQEXCHANGE_KEY = "mqExchange";

    private static final String MQROUTINGKEY_KEY = "mqRoutingKey";

    private static final String MQQUEUENAME_KEY = "mqQueueName";

    private static final String ACTORNUM_KEY = "actorNum";

    private static final String HOSTNAME_KEY = "wsHostName";

    private static final String PORT_KEY = "port";

    private static final String REDIS_URL = "redisURL";

    private static final String REDIS_MSG_KEY = "redisMsgKey"; //消息的KEY

    private static final Logger log = LoggerFactory.getLogger (WSConfig.class);

    public WSConfig () {

        if (properties == null) {
            properties = new Properties ();
            try {
                properties.load (new FileInputStream (CONFIG_FILE));
            }
            catch (IOException e) {
                e.printStackTrace ();
            }
            Map<String,String> env = System.getenv();
            this.properties.stringPropertyNames().forEach(key -> {
                String envKey = key.replace(".","_").toUpperCase();
                if(env.containsKey(envKey) && !env.get(envKey).isEmpty()){
                    log.info("Replace default config key {} with environment variable.", key);
                    this.properties.setProperty(key,env.get(envKey));
                }
            });
        }
    }

    public String getExchangeName () {

        return properties.getProperty (MQEXCHANGE_KEY);
    }

    public String getRoutingKey () {

        return properties.getProperty (MQROUTINGKEY_KEY);
    }

    public String getQueueName () {

        return properties.getProperty (MQQUEUENAME_KEY);
    }

    public int getActorNum () {

        return Tools.getInt (properties.getProperty (ACTORNUM_KEY));
    }

    public String getHostName () {

        return properties.getProperty (HOSTNAME_KEY);
    }

    public int getPort () {

        return Tools.getInt (properties.getProperty (PORT_KEY));
    }

    public String getRedisURl () {

        return properties.getProperty (REDIS_URL);
    }

    public String getRedisMsgKey () {

        return properties.getProperty (REDIS_MSG_KEY);
    }
}
